#!/bin/sh
# sw - suckless webframework - 2012 - MIT License - nibble <develsec.org>
# swx - Modified by Thuban - 2016 - MIT License -  <thuban@yeuxdelibad.net>
VERSION=0.4

preparebar() {
# $1 - bar length
# $2 - bar char
    barlen=$1
    barspaces=$(printf "%*s" "$1")
    barchars=$(printf "%*s" "$1" | tr ' ' "$2")
}

progressbar() {
# $1 - number (-1 for clearing the bar)
# $2 - max number
    if [ $1 -eq -1 ]; then
        printf "\r  $barspaces\r"
    else
        barch=$(($1*barlen/$2))
        barsp=$((barlen-barch))
        printf "\r[%.${barch}s%.${barsp}s]\r" "$barchars" "$barspaces"
    fi
}

register() {
    echo $1 >> "$LOG"
}

isfresher() {
    if [ -z $(uname |grep "BSD") ]; then
        if [ ! -f "${2}" ]; then
            register "$1"
            return 0
        elif [ $(stat "${1}" -c %Y) -gt $(stat "${2}" -c %Y) ]; then
            return 0
        else
            return 1
        fi
    else
        if [ ! -f "${2}" ]; then
            register "$1"
            return 0
        elif [ $(stat -f %m "${1}") -gt $(stat -f %m "${2}") ]; then
            return 0
        else
            return 1
        fi
    fi
}

domenulist() {

    echo "<p>Pages présentes dans ce dossier :</p>"
	echo "<ul>"
    for f in $(ls $(dirname $1)/) ; do
		sw_filter $i && continue
        [ -n "$(echo $f | grep 'index.$EXT$')" ] && continue
        echo "<li><a href=\"$(echo $f | sed -e "s,$EXT$,.html,g") \">$(echo $f | sed -e 's/\..*$//' -e 's/_/ /g')</a></li>"
    done
    echo "</ul>"

}

sw_filter() {
	for b in $BL; do
		[ "$b" = "$1" ] && return 0
	done
}

sw_main() {
    if [ -z $(uname |grep "BSD") ]; then
        if [ $(stat "$1" -c %s) -eq 0 ]; then
            domenulist $1
        else
            $CONVERTER $1 
        fi
    else
        if [ $(stat -f %z "$1") -eq 0 ]; then
            domenulist $1
        else
            $CONVERTER $1 
        fi
    fi
}


sw_menu() {
	echo "<ul>"
    if [ "$(dirname $1)" != "$IDIR" ] ; then
        echo "<li><a $( [ -n "$(echo $1 | grep index$EXT)" ] && echo "class=\"thisPage\" ") href=\"./index.html\">.</a></li>"
        echo "<li><a href=\"../index.html\">..</a></li>" 
    fi
	FILES=$(ls $(dirname $1) | sed -e "s,$EXT$,.html,g")
    INAME=$(basename "$(echo $1 | sed -e 's/\..*$//' -e 's/_/ /g')")
    
	for i in $FILES ; do
		sw_filter $i && continue
        NAME=$(echo $i | sed -e 's/\..*$//' -e 's/_/ /g')
        [ -n "$(echo $i | grep 'index.html$')" ] && continue
        #[ -z "$(echo $i | grep '\..*$')" ] && i="$i/index.html" # Pas d'extension : dossier

        echo "<li><a $( [ "$INAME" = "$NAME" ] && echo "class=\"thisPage\" ") href=\"$i\">$NAME</a></li>"
	done
	echo "</ul>"
}

sw_page() {
PAGENAME=$(basename "$1" |sed -e 's/\..*$//' -e 's/_/ /g')
DIRNAME=$(basename "$(dirname "$1" |sed -e 's/_/ /g')")
	# Header
	cat << _header_
<!doctype html>
<html>
<head>
<title>${DIRNAME} - ${PAGENAME} | ${TITLE}</title>
$HEADER
</head>
<body>
_header_
	echo "<main>"
	echo "$TOPTEMPLATE"
	sw_main $1
	echo "</main>"
	echo "$BOTTOMTEMPLATE"
	# Footer
	cat << _footer_
<footer>
${FOOTER}
</footer>
</body>
</html>
_footer_
}

# Set input dir
IDIR="$(echo $1 | sed -e 's,/*$,,')"
if [ -z "$IDIR" ] || [ ! -d $IDIR ]; then
    echo "swx - version $VERSION"
	echo "Usage: swx [dir]"
	exit 1
fi

# Load config file
echo "> Load configuration"
if [ ! -f $PWD/swx.conf ]; then
	echo "Cannot find swx.conf in current directory"
	exit 1
fi
. $PWD/swx.conf

echo "> Prepare output directory"
# Setup output dir structure
ODIR="$PWD/$(basename $IDIR).static"
mkdir -p $ODIR

# copy only if necessary
echo "> Copying files"
if [ -z $(uname |grep "BSD") ]; then
    cp -ur $IDIR/* $ODIR/
else
    #rsync -av $IDIR/ $ODIR/
    cp -r $IDIR/* $ODIR/
fi


# remove .EXT files
echo "> Removing useless files"
find $ODIR -iname "*$EXT" -exec rm '{}' \;

# Parse files
echo "> Generating html pages"
FILES=$(find $IDIR -iname "*$EXT" | sed -e 's,^\./,,')

NB=$(echo $FILES | wc -w)
COUNT=0

preparebar 50 "#"

for a in $FILES; do
    b="$(echo ${a%.*}.html | sed "s;$IDIR;$ODIR;g")"
	# process only new files
    isfresher "${a}" "${b}"
    if [ $? -eq 0 ]; then
        sw_page "${a}" > "${b}";
    fi

    # Progression
    #COUNT=$(expr $COUNT + 1)
    #PERC=$(expr $COUNT \* 100 / $NB )
    #if [ $PERC -eq 0 ]; then
    #    PERC=1
    #fi
    progressbar $COUNT $NB
    #BAR=$(echo "$FULLBAR" | cut -b -$PERC)
    #echo -n "($COUNT/$NB) $PERC% |$BAR| \r"
done

echo "\n\n> Done! :)"


exit 0
